﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Windows.Media.Imaging;
using System.Windows.Data;
using FluxJpeg.Core;
using System.IO;
using FluxJpeg.Core.Encoder;

namespace SUMO.MediaBrowser
{

    public class ImageSourceConverter : IValueConverter
    {
        #region IValueConverter

        public object Convert(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            string path = (string)value;
            BitmapImage result = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
            return result;

        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class Util
    {
        public static string ServiceBase
        {
            get;
            set;
        }

        public static string User
        {
            get;
            set;
        }

        public static string Password
        {
            get;
            set;
        }

        private const string jsDebug = @"function _dbg(text)
                {        // VS script debugger output window.
                        if ((typeof(Debug) !== 'undefined') && Debug.writeln) {
                            Debug.writeln(text);
                        }
                        // FF firebug and Safari console.
                        if (window.console && window.console.log) {
                            window.console.log(text);
                        }
                        // Opera console.
                        if (window.opera) {
                            window.opera.postError(text);
                        }
                        // WebDevHelper console.
                        if (window.debugService) {
                            window.debugService.trace(text);
                        }
                };";
        private const string jsDebugExec = "_dbg('{0}');";
        public static void DebugWrite(string text)
        {
            System.Diagnostics.Debug.WriteLine(text);
            HtmlPage.Window.Eval(jsDebug + string.Format(jsDebugExec, (text ?? "").Replace("\\", "\\\\").Replace("'", "\\'")));
            
        }

        public static byte[] GetBase64Jpg(WriteableBitmap bitmap)
        {
            int width = bitmap.PixelWidth;
            int height = bitmap.PixelHeight;
            int bands = 3;
            byte[][,] raster = new byte[bands][,];

            for (int i = 0; i < bands; i++)
            {
                raster[i] = new byte[width, height];
            }

            for (int row = 0; row < height; row++)
            {
                for (int column = 0; column < width; column++)
                {
                    int pixel = bitmap.Pixels[width * row + column];
                    raster[0][column, row] = (byte)(pixel >> 16);
                    raster[1][column, row] = (byte)(pixel >> 8);
                    raster[2][column, row] = (byte)pixel;
                }
            }

            ColorModel model = new ColorModel { colorspace = ColorSpace.RGB };
            FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);
            MemoryStream stream = new MemoryStream();
            JpegEncoder encoder = new JpegEncoder(img, 90, stream);
            encoder.Encode();

            stream.Seek(0, SeekOrigin.Begin);
            byte[] binaryData = new Byte[stream.Length];



             
            long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);
            return binaryData;

            /*
            string base64String =
                    System.Convert.ToBase64String(binaryData,
                                                  0,
                                                  binaryData.Length);

            return base64String;
            */
        }

    }
}
